﻿<%@ WebHandler Language="C#" Class="list" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using System.Web.Services;

public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest (HttpContext context) {
        string type = PageBase.ext_string(context.Request.Params["type"]);

        switch (type)
        {
           //查询部位下拉选项
            case "getselect_bw":
                getselect_bw(context);
                break;
           //查询尺码下拉选项
            case "getselect_cm":
                getselect_cm(context);
                break;
            //新增
            case "add":
                add(context);
                break;
            //加载数据
            case "getlist":
                getinfo_list(context);
                break;
            //删除
            case "delete":
                delete(context);
                break;
            //修改
            case "edit":
                edit(context);
                break;
            //根据id获取信息
            case "getlist_byid":
                getlist_byid(context);
                break;
            default:
                //getinfo(context);
                break;
        }
    }
   //查询部位下拉选项
    public void getselect_bw(HttpContext context)
    {
        string sql = @"select * from [t_bw_config] where isactive='Y'";
        DataTable dt = DBHelper.ExecuteDataTable(sql);
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"bwothername\":\"" + dt.Rows[i]["bwothername"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询尺码下拉选项
    public void getselect_cm(HttpContext context)
    {
        string sql = @"select * from [t_size] where isactive='Y'";
        DataTable dt = DBHelper.ExecuteDataTable(sql);
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"cmwm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //修改
    public void edit(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string id = Utility.Trim(list.LoadData_type[0].id);
        string sel_bw = Utility.Trim(list.LoadData_type[0].sel_bw);
        string sel_cm = Utility.Trim(list.LoadData_type[0].sel_cm);
        string bwvalue = Utility.Trim(list.LoadData_type[0].bwvalue);
        string isactive = Utility.Trim(list.LoadData_type[0].isactive);
        string isactive_= isactive == "True" ? "Y" : "N";

        string ishave = "select * from [t_cm_config] where bwid='{0}' and value='{1}' and isactive='Y'";
        ishave = string.Format(ishave,sel_bw, bwvalue);
        DataTable dtcount= DBHelper.ExecuteDataTable(ishave);
        if (dtcount.Rows.Count > 0 && dtcount.Rows[0]["id"].ToString() != id)
        {
            flag = false;
            msg = "已存在此名称，请修改";

        }
        else
        {
            sqlsave = @"update [t_cm_config] set 
                                   [bwid]='{0}'
                                  ,[cmid]='{1}'
                                  ,[value]='{2}'
                                  ,[isactive]='{3}'
                                  ,[modifytime]='{4}'
                                   where id={5};";
            sqlsave = string.Format(sqlsave, sel_bw, sel_cm, bwvalue, isactive_, DateTime.Now.ToString(),id);
            int zxcount = DBHelper.ExecuteNonQuery(sqlsave);
            if (zxcount > 0)
            {
                flag = true;
                msg = "修改成功";
            }
            else
            {
                flag = false;
                msg = "修改失败，请联系管理员";
            }
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询部位信息
    public void getinfo_list(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);

        string orderby = "createtime";
        string where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,a.id,b.id bwid,b.bwothername,a.value,c.id cmid,c.cmwm,c.cmnm,
                         ltrim(rtrim(a.isactive)) isactive,c.createtime,a.modifytime from 
                       [dbo].[t_cm_config] a left join [t_bw_config] b on a.bwid=b.id
                        left join t_size c on a.cmid=c.id  where a.isactive='Y' and 1=1 ";
        string sel_bw = PageBase.ext_string(context.Request.Params["sel_bw"]);//部位id
        string sel_cm = PageBase.ext_string(context.Request.Params["sel_cm"]);//尺码id
                    string bwvalue = PageBase.ext_string(context.Request.Params["bwvalue"]);//部位尺码值
            

        //部位id
        if (!string.IsNullOrEmpty(sel_bw))
        {
            where += " and b.id ='" + sel_bw + "'";
        }
        //尺码id
        if (!string.IsNullOrEmpty(sel_cm))
        {
            where += " and c.id ='" + sel_cm + "'";
        }
                //部位尺码值
        if (!string.IsNullOrEmpty(bwvalue))
        {
            where += " and a.value like N'%" + bwvalue + "%'";
        }
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");

        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"bwid\":\"" + dt.Rows[i]["bwid"] + "\"");
            sb.Append(",\"bwothername\":\"" + dt.Rows[i]["bwothername"] + "\"");
            sb.Append(",\"value\":\"" + dt.Rows[i]["value"] + "\"");
            sb.Append(",\"cmid\":\"" + dt.Rows[i]["cmid"] + "\"");
            sb.Append(",\"cmwm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"cmnm\":\"" + dt.Rows[i]["cmnm"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"modifytime\":\"" + dt.Rows[i]["modifytime"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //查询特体信息根据id
    public void getlist_byid(HttpContext context)
    {
        string editid = PageBase.ext_string(context.Request.Params["editid"]);
        string sql = @"select id, bwid, value, cmid, ltrim(rtrim(isactive)) as isactive, createtime, modifytime
                      from [t_cm_config] where id={0}";
        sql = string.Format(sql,editid);
        DataTable dt = DBHelper.ExecuteDataTable(sql);

        sb = new StringBuilder();
        string code = "0";
        string msg = "";

        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"bwid\":\"" + dt.Rows[i]["bwid"] + "\"");
            sb.Append(",\"value\":\"" + dt.Rows[i]["value"] + "\"");
            sb.Append(",\"cmid\":\"" + dt.Rows[i]["cmid"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"modifytime\":\"" + dt.Rows[i]["modifytime"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //删除
    public void delete(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "update t_cm_config set isactive='N' where id in ({0})";
        sqldelete = string.Format(sqldelete, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "删除成功";
        }
        else
        {
            flag = false;
            msg = "删除失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //新增
    public void add(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string sel_bw = Utility.Trim(list.LoadData_type[0].sel_bw);
        string sel_cm = Utility.Trim(list.LoadData_type[0].sel_cm);
        string bwvalue = Utility.Trim(list.LoadData_type[0].bwvalue);
        string isactive = Utility.Trim(list.LoadData_type[0].isactive);
        string isactive_= isactive == "True" ? "Y" : "N";
        string ishave = "select * from [t_cm_config] where bwid='{0}' and value='{1}' and isactive='Y'";
        ishave = string.Format(ishave,sel_bw,bwvalue);
        DataTable dtcount= DBHelper.ExecuteDataTable(ishave);
        if (dtcount.Rows.Count > 0)
        {
            flag = false;
            msg = "已存在此部位名称，请修改";
        }
        else
        {
            sqlsave = @"INSERT INTO [t_cm_config]
                                   ([bwid]
                                   ,[cmid]
                                   ,[value]
                                   ,[isactive]
                                   ,[createtime]
                                   )
                             VALUES
                                   ('{0}'
                                   ,'{1}'
                                   ,'{2}'
                                   ,'{3}'
                                   ,'{4}'
                                  );";
            sqlsave = string.Format(sqlsave, sel_bw, sel_cm, bwvalue, isactive_, DateTime.Now.ToString());
            int zxcount = DBHelper.ExecuteNonQuery(sqlsave);
            if (zxcount > 0)
            {
                flag = true;
                msg = "新增成功";
            }
            else
            {
                flag = false;
                msg = "新增失败，请联系管理员";
            }
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    public class GetDate
    {
        public List<typemanager> LoadData_type { get; set; }//总经理批示
    }
    public class typemanager
    {
        //id
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        //部位
        public string bwothername;
        public string BWOTHERNAME
        {
            get { return bwothername; }
            set { bwothername = value; }
        }
                    public string sel_bw;
        public string SEL_BW
        {
            get { return sel_bw; }
            set { sel_bw = value; }
        }
        public string sel_cm;
        public string SEL_CM
        {
            get { return sel_cm; }
            set { sel_cm = value; }
        }

        public string cmwm;
        public string CMWM
        {
            get { return cmwm; }
            set { cmwm = value; }
        }
        public string cmnm;
        public string CMNM
        {
            get { return cmnm; }
            set { cmnm = value; }
        }
        public string bwvalue;
        public string BWVALUE
        {
            get { return bwvalue; }
            set { bwvalue = value; }
        }
       
        public string isactive;
        public string ISACTIVE
        {
            get { return isactive; }
            set { isactive = value; }
        }
    }


    public bool IsReusable {
        get {
            return false;
        }
    }

}